const fs = require('fs');
const os = require('os');
const path = require('path');
const {
  promisify
} = require('util');

const namePath = path.join(__dirname, './data/name.txt');
const titlePath = path.join(__dirname, './data/title.txt');

const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);

readFile(namePath).then(function (data) {
  let arr = data.toString().split(os.EOL);
  let arr2 = [];
  let arr3 = [];
  for(let value of arr) {
    arr2 = value.split(' ');
    for(let item of arr2) {
      arr3.push(item);
    }
  }
  writeFile(path.join(__dirname, './data/names.txt'), arr3.toString()).then(()=>{
    console.log('finish');
  });
});

readFile(titlePath).then((data)=>{
  let arr = data.toString().split(os.EOL);
  let arr2 = [];
  let arr3 = [];
  for(let value of arr) {
    arr2 = value.trim().split(',');
    for(let item of arr2) {
      arr3.push(item.trim());
    }
  }
  console.log(arr3.length);
  writeFile(path.join(__dirname, './data/titles.txt'), arr3.toString()).then(()=>{
    console.log('finish');
  });
});